//
//  WBHomeViewController.swift
//  Weibo
//
//  Created by 伏文东 on 2018/1/22.
//  Copyright © 2018年 伏文东. All rights reserved.
//

import UIKit

private let kHomeCellID = "kHomeCellID"
class WBHomeViewController: WBBaseViewController {

    private lazy var listViewModel = WBStatusListViewModel()
    
    override func loadData() {
        print("准备刷新，最后一条数据\(self.listViewModel.statusList.last?.text ?? "没有数据了")")
        listViewModel.loadStatus(pullup: self.isPullup) { (isSuccessm, shouldRefresh) in
            
            self.refreshControl?.endRefreshing()
            self.isPullup = false
            if shouldRefresh {
               self.tableView?.reloadData()
            }
        }
    }
    
    @objc private func showFriends() {
        let vc = WBDemoViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
}
// MARK:- 表格数据源方法
extension WBHomeViewController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: kHomeCellID, for: indexPath)
        cell.textLabel?.text = listViewModel.statusList[indexPath.row].text
        return cell
    }
}
// MARK:- 设置界面
extension WBHomeViewController {
    
    override func setupTableView() {
        super.setupTableView()
        
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
        // 注册cell
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: kHomeCellID)
        setupNavTitle()
    }
    
    /// 设置导航栏标题
    private func setupNavTitle() {
        
        let title = WBNetworkManager.shared.userAccount.screen_name
        
        let button = WBTitleButton(title: title)
        
        navItem.titleView = button
        
        button.addTarget(self, action: #selector(clickTitleButton), for: .touchUpInside)
    }
    
    @objc func clickTitleButton(btn: UIButton) {
        // 设置选中状态
        btn.isSelected = !btn.isSelected
    }
    
}






























